﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCarERP.Models;
namespace WebCarERP.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.Title = "汽车配件";
            var info = db.autoParts.AsEnumerable().Where(t => t.Flag == 1);

            return View(info);

            // return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult add(autoPart entity)
        {
            entity.Flag = 1;
            entity.addtime = DateTime.Now;
            entity.chargeRemark = CurrentUserInfo.UName;
            db.autoParts.Add(entity);
            int resultCount = db.SaveChanges();

            resultCount = db.SaveChanges();
            var info = db.autoParts.AsEnumerable().Where(t => t.Flag == 1 && entity.addtime == t.addtime);
            //join y in db.SamplePersonInfo.AsEnumerable().Where(t => t.Flag == 1)
            //on x.InfoId equals y.InfoId

            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"添加成功\",\"data\":[" + json + "]}");
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult edit(autoPart entity)
        {
            autoPart AutoPart = db.autoParts.Find(entity.id);
            AutoPart.bianma = entity.bianma;
            AutoPart.name = entity.name;
            AutoPart.unit = entity.unit;
            AutoPart.price = entity.price;
            AutoPart.number = entity.number;
            AutoPart.desicrption = entity.desicrption;
            AutoPart.addtime = DateTime.Now;
            AutoPart.chargeRemark = CurrentUserInfo.UName;
            //Name = y.Name;
            db.Entry(AutoPart).State = EntityState.Modified;
            int resultCount = db.SaveChanges();
            var info = db.autoParts.AsEnumerable().Where(t => t.Flag == 1 && entity.addtime == t.addtime);

            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"修改成功\",\"data\":[" + json + "]}");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult delete(int? InfoId)
        {

            autoPart AutoPart = db.autoParts.Find(InfoId);
            AutoPart.Flag = 0;
            AutoPart.chargeRemark = CurrentUserInfo.UName;
            AutoPart.addtime = DateTime.Now;
            db.Entry(AutoPart).State = EntityState.Modified;

            int resultCount = db.SaveChanges();

            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"删除成功！\",\"data\":[]}");
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        /// <summary>
        /// 导出EXCEl
        /// </summary>
        /// <returns></returns>
        public FileResult ExportStu3()
        {
            string schoolname = "汽车配件";
            //创建Excel文件的对象
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            //添加一个sheet
            NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
            //获取list数据
            List<autoPart> listAutoPart = db.autoParts.AsEnumerable().Where(t => t.Flag == 1).ToList();

            //   Session["list1"] = null;
            //给sheet1添加第一行的头部标题
            NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
            row1.CreateCell(0).SetCellValue("编码");
            row1.CreateCell(1).SetCellValue("名称");
            row1.CreateCell(2).SetCellValue("单价");
            row1.CreateCell(3).SetCellValue("数量");
            row1.CreateCell(4).SetCellValue("单位");
            row1.CreateCell(5).SetCellValue("说明");
            //将数据逐步写入sheet1各个行
            for (int i = 0; i < listAutoPart.Count; i++)
            {
                NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                rowtemp.CreateCell(0).SetCellValue(listAutoPart[i].bianma.ToString());
                rowtemp.CreateCell(1).SetCellValue(listAutoPart[i].name.ToString());
                rowtemp.CreateCell(2).SetCellValue(listAutoPart[i].price.ToString());
                rowtemp.CreateCell(3).SetCellValue(listAutoPart[i].number.ToString());

                rowtemp.CreateCell(4).SetCellValue(listAutoPart[i].unit==null?"":listAutoPart[i].unit.ToString());
                rowtemp.CreateCell(5).SetCellValue(listAutoPart[i].desicrption == null ? "" : listAutoPart[i].desicrption.ToString());


            }
            // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", " 汽车配件 " + DateTime.Now + ".xls");

        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            //  WebSecurity.Logout();

            HttpContext.Cache.Remove(CurrentUserInfo.ID.ToString());
            CurrentUserInfo = null;
            Session.Clear();
            Session.Abandon();
            return RedirectToAction("Index", "Login");
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
